package main

import (
	"encoding/base64"
	"fmt"
	"image"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

func error(w http.ResponseWriter) {
	io.WriteString(w, "{\"status\":0}")
}

func success(w http.ResponseWriter) {
	io.WriteString(w, "{\"status\":1}")
}

func UploadImage(w http.ResponseWriter, req *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	content, err := ioutil.ReadAll(req.Body)
	if err != nil {
		error(w)
		return
	}
	fmt.Println(string(content))

	i := strings.Index(string(content), ",")

	reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(string(content)[i+1:]))
	image, name, err := image.Decode(reader)
	if err != nil {
		error(w)
		return
	}
	fmt.Println(name)
	fmt.Println(image.Bounds())
	//config.
	//fmt.Println("Width:", config.Width, "Height:", config.Height, "Format:", format)

	success(w)
}

func main() {
	http.HandleFunc("/upload", UploadImage)
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
